package net.studou.action;

import java.util.List;

import javax.annotation.Resource;

import net.studou.bean.tools.BeanUtils;
import net.studou.bean.tools.Properties;
import net.studou.exception.RechargeNoExistException;
import net.studou.exception.UserNotExistException;
import net.studou.po.IoLog;
import net.studou.po.Page;
import net.studou.po.User;
import net.studou.service.IoLogService;
import net.studou.tools.STDLogger;
import net.studou.vo.VIoLog;
import net.studou.vo.VMessage;
import net.studou.vo.VUser;

import org.springframework.stereotype.Component;

@Component("ioAction")
public class IOAction extends BasicAction {

	private static final long serialVersionUID = -7864662278384572216L;

	private STDLogger logger = STDLogger.getLogger(IOAction.class);

	@Resource(name = "ioLogService")
	private IoLogService ioLogService;

	private VIoLog ioLog;

	private Page page;

	private List<VIoLog> ioList;

	public String findIO() {
		User pUser = getUserFromSession("userId");

		IoLog pIoLog = new IoLog();
		String source = "ioType, status, begin, end";
		String target = "ioType, status, begin, end";
		BeanUtils.copyBean(ioLog, pIoLog, new Properties(source, target));

		ioList = ioLogService.findIO(pIoLog, pUser, page);

		return SUCCESS;
	}

	public String recharge() {
		IoLog pIoLog = new IoLog();
		BeanUtils.copyBean(ioLog, pIoLog, new Properties("ioNo"));

		VUser vUser = (VUser) getSession().getAttribute("userInfo");
		User pUser = new User();
		BeanUtils.copyBean(vUser, pUser, new Properties("userName"));

		String forward = null;

		try {
			ioLogService.recharge(pIoLog, pUser);
			forward = SUCCESS;
			setFeedBackMesssage(new VMessage(VMessage.SUCCESS, "充值成功", "请马上与充值客服联系，如果联系不上，平台会在24小时内处理。"));
		} catch (UserNotExistException e) {
			logger.debug(e);
			forward = INPUT;
			setFeedBackMesssage(new VMessage(VMessage.ERROR, "充值失败", "由于您长时间没有操作，为了保证账户安全，请重新登录。"));
		} catch (RechargeNoExistException e) {
			logger.debug(e);
			forward = INPUT;
			setFeedBackMesssage(new VMessage(VMessage.ERROR, "充值失败", "该充值号已经提交，请勿重复提交。"));
			setFieldErrorMessage("rechargeError", "该充值号已经提交，请勿重复提交。");
		}

		return forward;
	}

	public IoLogService getIoLogService() {
		return ioLogService;
	}

	public void setIoLogService(IoLogService ioLogService) {
		this.ioLogService = ioLogService;
	}

	public VIoLog getIoLog() {
		return ioLog;
	}

	public void setIoLog(VIoLog ioLog) {
		this.ioLog = ioLog;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<VIoLog> getIoList() {
		return ioList;
	}

	public void setIoList(List<VIoLog> ioList) {
		this.ioList = ioList;
	}

}
